<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>菜单新增</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="stylesheet" href="/html/layui/css/layui.css" media="all">
</head>
<body>

  <div class="layui-form" lay-filter="layuiadmin-app-form-list" id="layuiadmin-app-form-list" style="padding: 20px 30px 0 0;">
    <div class="layui-form-item">
      <input type="button" id="layuiadmin-app-form-import" value="批量导入">
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">菜单名称</label>
      <div class="layui-input-inline">
        <input type="text" name="name" id="name" lay-verify="required" placeholder="请输入菜单名称" autocomplete="off" class="layui-input">
      </div>
    </div>
     <div class="layui-form-item">
      <label class="layui-form-label">菜单地址</label>
      <div class="layui-input-inline">
        <input type="text" name="url" id="url" lay-verify="required" placeholder="请输入菜单地址" autocomplete="off" class="layui-input">
      </div>
    </div>
    <div class="layui-form-item layui-hide">
      <input type="hidden" id="parentId" name="parentId"/>
      <input type="hidden" id="id" name="id"/>
      <input type="button" lay-submit lay-filter="layuiadmin-app-form-submit" id="layuiadmin-app-form-submit" value="确认添加">
      <input type="button" lay-submit lay-filter="layuiadmin-app-form-edit" id="layuiadmin-app-form-edit" value="确认编辑">
    </div>
  </div>

  <script src="/html/layui/layui.js"></script>  
  <script>
  layui.config({
    base: '/html/' //静态资源所在路径
  }).extend({
    index: 'lib/index' //主入口模块
  }).use(['index', 'form'], function(){
    var $ = layui.$
    ,form = layui.form;
    
    function getQueryVariable(variable) {
      var query = window.location.search.substring(1);
      var vars = query.split("&");
      for (var i = 0; i < vars.length; i++) {
        var pair = vars[i].split("=");
        if (pair[0] == variable) {
          return pair[1];
        }
      }
      return (false);
    }
    var id = getQueryVariable("id");
    var parentId = getQueryVariable("parentId");
    if(parentId){
        $("#parentId").val(parentId);
    }
    if(id){
      $.ajax({
        async : true,
        "url" : "/api/menu/get?id="+id,
        "global" : false,
        "type" : "GET",
        "contentType": "application/json; charset=utf-8",
        "dataType" : "json",
        "timeout" : "60000",
        "traditional":true,
        "success" : function(a,b,c){
          if(a.v){
            var data=a.v[0]
            $("#id").val(data.id)
            $("#name").val(data.name)
            $("#url").val(data.url)
            $("#parentId").val(data.parentId)
          }
        },
        "error" : function(err, msg, code) {
          return false;
        }
      });
    }
    $("#layuiadmin-app-form-import").click(function(){
        parent.layer.open({
          type: 2
          ,title: '选择资源'
          ,content: '/html/views/resource/selectList.html'+"?parentId="+parentId
          ,maxmin: true
          ,area: ['550px', '550px']
          ,btn: ['确定', '取消']
          ,yes: function(index, layero){
            //点击确认触发 iframe 内容中的按钮提交
            var iframeWin = parent.window[layero.find('iframe')[0]['name']];
           
            var text=iframeWin.layui.table.checkStatus('id');
//             var submit = layero.find('iframe').contents().find("#layuiadmin-app-form-submit");
            debugger
             iframeWin.test();
//             var checkStatus = table.checkStatus('id');
//             submit.click();
          }
        }); 
    });
    //监听提交
    form.on('submit(layuiadmin-app-form-submit)', function(data){
      alert("1");
      var field = data.field; //获取提交的字段
      var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引  
			$.ajax({
		async : true,
		"url" : "/api/menu/add",
		"global" : false,
		"type" : "POST",
		"contentType": "application/json; charset=utf-8",
		"dataType" : "json",
		"timeout" : "60000",
		"data" : JSON.stringify(field),
		"traditional":true,
		"success" : function(a,b,c){
			parent.layui.table.reload('LAY-app-content-list'); //重载表格
      		parent.layer.close(index); //再执行关闭 
		},
		"error" : function(err, msg, code) {
			return false;
		}
	});
      //提交 Ajax 成功后，关闭当前弹层并重载表格
      //$.ajax({});
      
    });
  })
  </script>
</body>
</html>
			